<template>
    <div class="article-page">
        <div class="banner">
            <div class="container">

            <h1>{{article.title}}</h1>

            <article-meta :article="article"></article-meta>

            </div>
        </div>

        <div class="container page">

            <div class="row article-content">
            <div class="col-md-12" v-html="article.body">
            </div>
            </div>

            <hr />

            <div class="article-actions">
                <article-meta :article="article"></article-meta>
            </div>

            <div class="row">

            <div class="col-xs-12 col-md-8 offset-md-2">

                <form class="card comment-form" @submit.prevent='postComment(article.slug)'>
                <div class="card-block">
                    <textarea class="form-control" placeholder="Write a comment..." rows="3" v-model="comment.body"></textarea>
                </div>
                <div class="card-footer">
                    <img v-if="user" :src="user.image" class="comment-author-img" />
                    <img v-else src="https://csdnimg.cn/cdn/content-toolbar/csdn-logo.png?v=20200416.1" class="comment-author-img" />
                    <button class="btn btn-sm btn-primary">
                    Post Comment
                    </button>
                </div>
                </form>
                
                <div class="card" v-for="item in commentList" :key="item.id" v-if="commentList.length>0">
                    <div class="card-block">
                        <p class="card-text">{{item.body}}</p>
                    </div>
                    <div class="card-footer">
                        <nuxt-link 
                            :to="{
                                name:'profile',
                                params:{
                                    username:item.author.username
                                }
                            }" 
                            class="comment-author">
                        <img :src="item.author.image" class="comment-author-img" />
                        </nuxt-link>
                        &nbsp;
                        <nuxt-link  
                            :to="{
                                name:'profile',
                                params:{
                                    username:item.author.username
                                }
                            }"
                            class="comment-author"
                        >{{item.author.username}}</nuxt-link>
                        <span class="date-posted">{{item.updatedAt|date('MMM,DD')}}</span>
                        <span class="mod-options" @click="deleteComment(article.slug,item.id)"><i class="ion-trash-a" v-if="item.author.username==$store.state.user.username"></i></span>
                    </div>
                </div> 
            </div>

            </div>

        </div>
    </div>
</template>
<script>
import {getArticleDetail,getComments,addComments,deleteComments} from '@/api/article'
import MarkdownIt from 'markdown-it'
import ArticleMeta from './article_meta'
import {mapState} from 'vuex'
export default {
    name:'ArticleIndex',
    watchQuery:['slug'],//监听query参数变化
    async asyncData({params}){
        const { data } = await getArticleDetail(params.slug)
        const md = new MarkdownIt()
        const { article } = data
        article.body = md.render(article.body)
        article.favoriteDisabled = false
        const { data:comment } = await getComments(params.slug)
        return {
            article,
            commentList:comment.comments
        }
    },
    components:{
        ArticleMeta
    },
    computed:{
        ...mapState(['user'])
    },
    head() {
      return {
        title: `${this.article.title}-RealWorld`,
        meta: [
          {
            hid: this.article.slug,
            name: 'description',
            content: this.article.description
          }
        ]
      }
    },
    data(){
        return {
            comment:{
                body:''
            }
        }
    },
    methods:{
        async postComment(slug){
            const {data} = await addComments(slug,{comment:this.comment})
            if(data.comment){
                alert('post comment success!')
                const { data:comment } = await getComments(this.$route.params.slug)
                this.commentList = comment.comments
                this.comment.body=''
            }
        },
        async deleteComment(slug,id){
            let {data} = await deleteComments(slug,id)
            alert('delete comment success!')
            const { data:comment } = await getComments(this.$route.params.slug)
            this.commentList = comment.comments
        }
    }
}
</script>